﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SurveySystem.Model;
using SurveySystem.Repository;
using System.IO;

namespace SurveySystem.Factory
{
    public class CsvSurveyExportFactory
    {
        public const string CSV_SPLIT = ",";
        public const string CSV_NEW_LINE = "\n";

        private ISurveyRepository surveyRepository;
        private ISurveyQuestionRepository questionRepository;
        private ISurveyAnswerRepository answerRepository;
        private ISurveyResponseRepository responseRepository;
        private ISurveyResponseAnswerRepository responseAnswerRepository;

        public CsvSurveyExportFactory(ISurveyRepository surveyRepository, ISurveyQuestionRepository questionRepository, ISurveyAnswerRepository answerRepository, ISurveyResponseRepository responseRepository, ISurveyResponseAnswerRepository responseAnswerRepository)
        {
            this.surveyRepository = surveyRepository;
            this.questionRepository = questionRepository;
            this.answerRepository = answerRepository;
            this.responseRepository = responseRepository;
            this.responseAnswerRepository = responseAnswerRepository;
        }

        public string ExportSurveyAsCSV(int surveyId)
        {

            SurveyFactory surveyFactory = new SurveyFactory(surveyRepository, questionRepository, answerRepository);
            Survey survey = surveyFactory.GetPopulatedSurveyByID(surveyId);

            List<string> rowList = new List<string>();

            rowList.Add("SurveyID: " + CSV_SPLIT + survey.SurveyID + CSV_SPLIT + CSV_SPLIT + //
                "Title:" + CSV_SPLIT + survey.Title.Replace(CSV_SPLIT, "") + CSV_NEW_LINE);

            rowList.Add(survey.Description.Replace(CSV_SPLIT, "") + CSV_NEW_LINE);

            rowList.Add("Owner: " + CSV_SPLIT + survey.Owner.Replace(CSV_SPLIT, "") + CSV_NEW_LINE);

            rowList.Add("Survey Open Date: " + survey.OpenDate + CSV_SPLIT + DateTime.Now);

            rowList.Add(CSV_NEW_LINE);
            rowList.Add(CSV_NEW_LINE);

            foreach (Question q in survey.Questions)
            {
                string questionDescriptionRow = "Question " + q.QuestionNumber + ": " + CSV_SPLIT + q.QuestionText.Replace(CSV_SPLIT, "") + CSV_NEW_LINE;
                string answerDescriptionsRow = "";
                string answerResultsRow = "";

                foreach (Answer a in q.PossibleAnswers)
                {

                    answerDescriptionsRow = answerDescriptionsRow + a.AnswerText.Replace(CSV_SPLIT, "") + CSV_SPLIT;

                    int answerCount = responseAnswerRepository.GetResponseAnswersForAnswer(a.AnswerID).Count;
                    answerResultsRow = answerResultsRow + answerCount + CSV_SPLIT;

                }

                answerDescriptionsRow = answerDescriptionsRow + CSV_NEW_LINE;
                answerResultsRow = answerResultsRow + CSV_NEW_LINE;

                rowList.Add(questionDescriptionRow);
                rowList.Add(answerDescriptionsRow);
                rowList.Add(answerResultsRow);
                rowList.Add(CSV_NEW_LINE);

            }

            string csvString = "";

            foreach (string row in rowList)
            {
                csvString = csvString + row;
            }

            return csvString;
        }

        public void ExportSurveyToCsvFile(int surveyID, string filename)
        {
            string output = ExportSurveyAsCSV(surveyID);

            TextWriter tws = new StreamWriter(filename);
            tws.Write(output);
            tws.Close();   // close the stream

        }

    }
}
